package com.i2works.smartluolong.manage.task;

import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.flow.FlowInfoQuery;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.xiaoleilu.hutool.date.DateUnit;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 事项超期监控
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/6/21 10:55
 */
@Component
@Configurable
//标注启动定时任务
@EnableScheduling
public class MonitorScheduleTask {

    /**
     * 状态
     */
    @Value("${monitor.task.status}")
    private Boolean taskStatus;

    @Autowired
    private IFlowInfoService flowInfoService;

    //    每5000毫秒（5秒）执行一次
    //    @Scheduled(fixedRate = 5000)
    //    @Deprecated
    //    public void reportCurrentTime() {
    //        System.err.println("任务调度启动中: 当前时间" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
    //    }

    /**
     * 扫描事项办理时效（周1-周五，每天9和13点各扫描一次）
     */
    @Scheduled(cron = "${monitor.task.cron}")
    public void scan() throws ServiceException {
        if (taskStatus) {
            FlowInfoQuery flowInfoQuery = new FlowInfoQuery();
            flowInfoQuery.setComplete(0);
            flowInfoQuery.setLimit(200L);
            List<FlowInfoDO> doList = flowInfoService.queryListByMonitor(flowInfoQuery);
            for (FlowInfoDO flowInfoDO : doList) {
                //得到剩余天数
                if (flowInfoDO.getDeadline() == null) {
                    continue;
                }
                Long betweenDay = DateUtil.between(new Date(), flowInfoDO.getDeadline(), DateUnit.DAY);
                //得到预警级别
                Integer termLevel = getTermLevel(flowInfoDO.getPromiseTerm(), betweenDay);
                if (!termLevel.equals(flowInfoDO.getTermLevel())) {
                    //更新数据
                    FlowInfoDO infoDO = new FlowInfoDO();
                    infoDO.setId(flowInfoDO.getId());
                    infoDO.setTermLevel(termLevel);
                    flowInfoService.update(infoDO);
                }
            }
            System.err.println("事项超期监控....");
        }
    }

    /**
     * 计算预警级别
     *
     * @param promiseTerm 总期限
     * @param betweenDay  剩余几天
     */
    private static Integer getTermLevel(Integer promiseTerm, Long betweenDay) {
        if (betweenDay == 0) {
            return 3;
        }
        if (promiseTerm == null) {
            return 1;
        }
        //每份的数量
        BigDecimal average = new BigDecimal(promiseTerm).divide(new BigDecimal(3), BigDecimal.ROUND_UP);
        String toString = new BigDecimal(betweenDay).divide(average, BigDecimal.ROUND_UP).toString();
        String toString2 = average.divide(new BigDecimal(betweenDay), BigDecimal.ROUND_UP).toString();
        int parseInt = Integer.parseInt(toString);
        switch (parseInt) {
            case 1:
                return 3;
            case 2:
                return 2;
            case 3:
                return 1;
            default:
                return 1;
        }
    }


}

